// HelpDialog.cpp : implementation file
//

#include "stdafx.h"
#include "8085.h"
#include "HelpDialog.h"
#include "globalFunctions.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHelpDialog dialog


CHelpDialog::CHelpDialog(CString szHelpFile,CString szTitle,CWnd* pParent /*=NULL*/)
	: CDialog(CHelpDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHelpDialog)
	m_szHelpData = _T("");
	//}}AFX_DATA_INIT
    m_szHelpFile	= szHelpFile;
    m_szTitle		= szTitle;
	m_canShow		= FALSE;
}


void CHelpDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHelpDialog)
	DDX_Text(pDX, IDC_HELP_DATA, m_szHelpData);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CHelpDialog, CDialog)
	//{{AFX_MSG_MAP(CHelpDialog)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHelpDialog message handlers

BOOL CHelpDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
    SetWindowText(m_szTitle);
    ReadFile(m_szHelpFile);

    UpdateData(FALSE);
    return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


// open file <install dir>\<szFileName> return FALSE if unsucessful.
BOOL CHelpDialog::ReadFile(const CString& szFileName)
{
    char startDir[_MAX_PATH+1];
    CString help;
    GetModuleFileName(NULL,startDir,_MAX_PATH);
	int i=0;
    // trace backward till the name of the executable file
    // starts
    for(i=strlen(startDir); i>0 && startDir[i]!='\\'; i--)
                ;
    startDir[i]=0;
    //sprintf(help,"\"%s\\Help\\%s\"",startDir);
    help.Format("%s\\Help\\%s",startDir,szFileName);

    CFile f;
    if(!f.Open(help,CFile::modeRead))
    {
        char str[1024];
        sprintf(str,"Could not open <%s>",help);
        ::ShowErrorMessage(str);
		m_canShow = FALSE;
        return FALSE;
    }

    char buf[2*1024];
    f.Read(buf,sizeof(buf)-1);
    m_szHelpData = buf;
	m_canShow = TRUE;
    return TRUE;
}


BOOL CHelpDialog::CanShow()
{
	return m_canShow;
}
